local zhengu = fk.CreateSkill({
    name = "sj3__zhengu",
})
  
  Fk:loadTranslationTable{
    ["sj3__zhengu"] = "镇骨",
    [":sj3__zhengu"] = "每回合限一次，当你使用<a href='jishipai_js'>即时牌</a>时，你可以将一项调整至与另一项相等（变化量至多为5）：<br>1.目标数；2.手牌数；3.3。",
    ["#sj3__mubiaoshu"] = "目标数",
    ["#sj3__shoupaishu"] = "手牌数",
    ["@sj3__zhengu"] = "镇骨",
    ["#sj3__tiaozheng"] = "镇骨：将%arg调整至%arg2（%arg3）",
    ["#sj3__zhengu-jian"] = "镇骨：请减少%arg个目标",
    ["#sj3__zhengu-jia"] = "镇骨：请增加%arg个目标",
  
    ["$sj3__zhengu1"] = "镇守城池，必以骨相拼！",
    ["$sj3__zhengu2"] = "孔明计虽百算，却难敌吾镇骨千具！",
  }
  


  zhengu:addEffect(fk.CardUsing, {
    anim_type = "control",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(zhengu.name) and player:usedSkillTimes(zhengu.name) == 0 and
        (data.card.type == Card.TypeBasic or 
        (data.card.type == Card.TypeTrick and 
        data.card.sub_type ~= Card.SubtypeDelayedTrick))--即时牌
    end,
    on_cost = function (self, event, target, player, data)
      local tos = #data.tos
      local num = player:getHandcardNum()
      local mark = player:getMark("@sj3__zhengu")
      if tos == num and tos == mark and num == mark then return false end--全相等返回
      local n = {tos,num,mark}
      local text = {"#sj3__mubiaoshu","#sj3__shoupaishu","@sj3__zhengu"}
      local all,ask,b = {},{},{}
      for i, v in ipairs(text) do
        for j, u in ipairs(text) do
          if i ~= j then--变化数至多为5
            local bian = n[i] > n[j] and math.max(n[j],n[i] - 5) or math.min(n[j],n[i] + 5)
            local t = "#sj3__tiaozheng:::"..v..":"..u..":"..bian
            if n[i] == n[j] then
              t = "#sj3__tiaozheng:::"..v..":"..u..":"..n[i]
            else
              if i == 1 then
                if (tos + #data:getExtraTargets({bypass_distances = true})) >= bian then--合法目标数必须足够
                  table.insert(ask,t)
                  table.insert(b,bian)
                end
              else
                table.insert(ask,t)
                table.insert(b,bian)
              end
            end
            table.insert(all,t)
          end
        end
      end
      table.insert(ask,"Cancel")
      table.insert(all,"Cancel")
      local choices = player.room:askToChoice(player,{
        choices = ask,
        skill_name = zhengu.name,
        all_choices = all,
        detailed = false,--详细描述会无法识别%arg
        --cancelable = true --无效
      })
      if choices == "Cancel" then
        return false
      else
        for i, v in ipairs(all) do
          if choices == v then
            for j, u in ipairs(ask) do
              if choices == u then
                event:setCostData(self,{xuan = i,bian = b[j]})
                return true
              end
            end
          end
        end
      end
    end,
    on_use = function(self, event, target, player, data)
      local cost = event:getCostData(self)
      local room = player.room
      if cost.xuan < 3 then --调整目标
        if #data.tos > cost.bian then
          local ts = data.tos
          if cost.bian > 0 then--目标为0不用选
          local n = #data.tos - cost.bian
            ts = room:askToChoosePlayers(player,{
              targets = ts,
              min_num = n,
              max_num = n,
              skill_name = zhengu.name,
              prompt = "#sj3__zhengu-jian:::"..n,
              cancelable = false,
            })
          end
          for _, t in ipairs(ts) do
            data:removeTarget(t)
          end
        else
          local n = cost.bian - #data.tos
          local ts = room:askToChoosePlayers(player,{
            targets = data:getExtraTargets({bypass_distances = true}),
            min_num = n,
            max_num = n,
            skill_name = zhengu.name,
            prompt = "#sj3__zhengu-jia:::"..n,
            cancelable = false,
          })
          for _, t in ipairs(ts) do
            data:addTarget(t)
          end
        end
      elseif cost.xuan > 2 and cost.xuan < 5 then --调整手牌
        if player:getHandcardNum() > cost.bian then
          room:askToDiscard(player,{
            min_num = player:getHandcardNum() - cost.bian,
            max_num = player:getHandcardNum() - cost.bian,
            include_equip = false,
            skill_name = zhengu.name,
            cancelable = false,
          })
        else
          room:drawCards(player,cost.bian - player:getHandcardNum(),zhengu.name)
        end
      else--调整镇骨
        room:setPlayerMark(player,"@sj3__zhengu",cost.bian)
      end
    end,
  })

  zhengu.onAcquire = function (self, player, is_start)
    player.room:setPlayerMark(player,"@sj3__zhengu",3)
  end
  zhengu.onLose = function (self, player, is_start)
    player.room:setPlayerMark(player,"@sj3__zhengu",0)
  end

  return zhengu